$(function(){
	var flownumber = getUrlParam('flownumber');
	
	getNode(flownumber);
	
	
	$('#flownextnode tbody').on('change', '.node', function(){	
		var isChecked = $(this).prop('checked'); 
		
		var nodecode = $(this).attr('nodecode');

		if(isChecked)
			setCheckNextNode(nodecode);
	})
	//将下一节点选中的节点代码，保存到已选中的‘流程节点’中
	function setCheckNextNode(nodecode){
		$('#flownode tbody').find('.node').each(function(){
			var isCheck = $(this).prop('checked');
			if(isCheck){
				var nextnodecode = "";// = $(this).attr('nextnodecode');
//				if(nextnodecode == undefined)
//					nextnodecode = nodecode;
////				else{
					$('#flownextnode tbody').find('.node').each(function(){
						if($(this).prop('checked')){
							nextnodecode += $(this).attr('nodecode') + ',';
						}
					})
					nextnodecode = nextnodecode.substring(0, nextnodecode.length - 1);
//				}
				
				$(this).attr('nextnodecode', nextnodecode);
				return true;
			}
		})
	}
})


var nodes = null;
function getNode(flownumber){
	
	$.ajax({ 
		url: projectPath + "flow/getnodes.do",
		data: {"flownumber": flownumber},
		dataType: "json",
		success: function(data){	            						
			if(data.result == 1){
				nodes = data.datas;
				
				bindNode(nodes);
			}
      	}
	});
}



function bindNode(nodes){
	for(var i = 0; i < nodes.length; i++){
		
		var trStr = '<tr style="text-align: center;" class="node">'+
				    '    <td>'+
				    		nodes[i]["serialnumber"]+
				    '    </td>'+
				    '    <td>'+
			    	'		<span class="nodecode">'+ nodes[i]["nodecode"] +'</span> '+ 
			        '	 </td>'+
				    '    <td>'+
			    			nodes[i]["nodename"]+
				    '    </td>'+
				    '    <td>'+
				    '    	<select class="combox nextnodecode" id="nextnode_'+ i +'" name="nextnodecode_'+ i +'" multiple>   '+
	                '		</select> '+
			        '	 </td>'+
				    '</tr>';

		$('#flownode').find('tbody').append(trStr);
		

	    $('#nextnode_' + i + '').select2({
			placeholder : "请选择下一节点",
			tags : true,
			createTag : function(decorated, params) {
				return null;
			},  
	        width:'100%'  
		}); 
	    
	    getNextNode('nextnode_'+ i, nodes[i]["nodecode"]);
	}

    bindNodeCode(nodes);
}

function bindNodeCode(nodes){
	// 先清空控件数据
	$('.nextnodecode').empty(); 

	var option = $("<option>").val("").text(""); 
	$('.nextnodecode').append(option); 
	
	for (var i = 0; i < nodes.length; i++) {
		var value = nodes[i].nodecode;
		var text = nodes[i].nodename;
		
		var option = $("<option>").val(value).text(text); 
		$('.nextnodecode').append(option); 
	}	
}

function bindNextNode(){

	$('#flownode tbody').find('.node').each(function(){
		var nodecode = $(this).attr('nodecode');
		
		getNextNode(nodecode);
	})
}

function getNextNode(objID, nodecode){
	
	$.ajax({ 
		url: projectPath + "flow/getnextnodes.do",
		data: {"nodecode": nodecode},
		dataType: "json",
		success: function(data){	    
			if(data.result == 1){
				var nextnodecode = "";
				var jsonnextnodecode = data.datas;
	            for(var i = 0; i < jsonnextnodecode.length; i++){
	            	nextnodecode += jsonnextnodecode[i].nextnodecode + ",";
	            }
	            nextnodecode = nextnodecode.substring(0, nextnodecode.length - 1).split(',');
//	            alert(nextnodecode);
	            $('#' + objID).val(nextnodecode).trigger("change");
			}
      	}
	});
}

function save(){
	var param = {};
	var data = [];
	$('#flownode').find('tbody').find('tr').each(function(){
		var nodecode = "";
		$(this).find('.nodecode').each(function(){
			nodecode = $(this).html();
		})
		
		var nextnodecodes = "";
		$(this).find('.nextnodecode').each(function(){
			if(null != $(this).val())
				nextnodecodes = $(this).val();
		})
		var nextnodecode = "";
		if("" != nextnodecodes){
			for(var k = 0; k < nextnodecodes.length; k++){
				nextnodecode+=nextnodecodes[k]+',';
			}
			nextnodecode = nextnodecode.substring(0, nextnodecode.length - 1);
		}
		
		var json = {};
		json['nodecode'] = nodecode;
		json['nextnodecode'] = nextnodecode;
		data.push(json);
	})
	
	param["datas"] = data;
	
	$.ajax({ 
		method: 'post',
		url: projectPath + "flow/savenextnode.do",
		data: JSON.stringify(param),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	            						
			if(data.result == 1){
	        	window.wxc.xcConfirm("保存成功！", window.wxc.xcConfirm.typeEnum.success);
			}
      	}
	});
}
